#include "mylineedit.h"
#include <QToolButton>
#include <QWidgetAction>
#include <QEvent>
#include <QFont>
#include <QDebug>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>

AttributeEdit::AttributeEdit(QWidget *parent)
    :QWidget(parent)
{
    setWindowFlags(Qt::FramelessWindowHint);
    setFocusPolicy(Qt::StrongFocus);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    setMaximumWidth(parentWidget()->maximumWidth());
    setMinimumWidth(parentWidget()->minimumWidth());

    label = new QLabel(this);
    label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    edit = new QLineEdit(this);
    edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    edit->installEventFilter(this);
    edit->setMaximumWidth(parentWidget()->maximumWidth());
    edit->setMinimumWidth(parentWidget()->minimumWidth());

    edit->setStyleSheet("border : none;"
                        "border-left : 1px solid lightgray;"
                        "background-color : transparent;");

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(label, 0, Qt::AlignHCenter | Qt::AlignVCenter);
    layout->addWidget(edit);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setStretchFactor(label, 1);
    layout->setStretchFactor(edit, 2);
    setLayout(layout);
    setContentsMargins(0, 0, 0, 0);

    connect(edit, &QLineEdit::editingFinished, this, &AttributeEdit::slt_editingFinished);
    show();

    m_attribute = "";
    m_value = "";
}

void AttributeEdit::slt_editingFinished()
{
    emit editingFinished(edit->text());
}

void AttributeEdit::setKey(QString key)
{
    label->setText(key);
    m_attribute = key;
}

void AttributeEdit::setValue(QString value)
{
    edit->setText(value);
    m_value = value;
}
